<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    @close="cancel"
  >
    <!-- 表单 -->
    <el-form ref="formData" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:90%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:90%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:90%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:90%" placeholder="请选择">
          <el-option v-for="item in employessEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:90%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:90%" placeholder="请选择部门" @focus="showTree=true" />
        <el-tree v-if="showTree" class="departmentList" :data="depts" :props="{label:'name'}" :default-expand-all="true" :expand-on-click-node="false" @node-click="nodeClick" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:90%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="cancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { addEmployee } from '@/api/employess'
import { listToTreeData } from '@/utils'
import { getDepartments } from '@/api/departments'
import employessEnum from '@/api/constant/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      employessEnum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { min: 2, message: '名称最小不能小于2字符', trigger: 'blur' },
          { max: 9, message: '名称最大不能大于9字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/, message: '请输入正确的11位手机号', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择日期', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '请选择聘用形式', trigger: 'change' }
        ],
        workNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: 'change' }
        ]
      },
      depts: [],
      showTree: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async  getDepartments() {
      // 1. 获取所有部门列表（扁平）
      const { depts } = await getDepartments()
      // 2.使用之前的树形转换封装函数
      this.depts = listToTreeData(depts, '')
    //   console.log(this.depts)
    },
    // 取消
    cancel() {
      // 1.清空表单
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 2.清空报错
      this.$refs.formData.resetFields()
      // 3.关闭弹层
      this.$emit('update:showDialog', false)
    },
    // 确认
    async btnOK() {
      // 1.校验表单
      await this.$refs.formData.validate()
      // 2.发起请求
      await addEmployee(this.formData)
      //   3.提示成功
      this.$message.success('添加成功')
      // 4.关闭弹窗
      this.$emit('update:showDialog', false)
      //   this.$parent.showDialog = false 这种方法要明确父子组件，否则容易报错
      //   5.更新数据
      this.$emit('addEmployee')
    //   this.$parent.getEmployessList()
    },
    // 选择部门
    nodeClick(data) {
      // 属性被点击的回调
      // 可以接受被点击的数据对象
    //   console.log(data)
      this.formData.departmentName = data.name
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped>
  .departmentList {
    border: 1px solid #aaa;
    width: 90%;
    position: absolute;
    z-index: 9;
    height: 200px;
    overflow: auto;
  }
</style>
